/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var ajaxRequest;
if (window.XMLHttpRequest)
{
	// code for IE7+, Firefox, Chrome, Opera, Safari
	ajaxRequest=new XMLHttpRequest();
}
else
{	
	// code for IE6, IE5
	ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP");
}

var productIdGlobal;

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState === 4){
		var response = ajaxRequest.responseText;
                var resInt = parseInt(response);
                console.log(resInt);
                if( resInt > 0 ){
                    //var quantity = parseInt(document.getElementById("q-"+productIdGlobal).value);
                    alert("Done");
                    document.getElementById("cart").innerHTML = response ;
                }else if( resInt === 0 ){
                    alert("Error Updating Your Cart .. Please try Again later");
                }else if( resInt === -1 )
                    alert("Sorry We Don't have Enough Quantity now .. Please try Again later");
                document.getElementById("q-"+productIdGlobal).value = 1;
	}
}

function addToCart(productId){
    
	var quantity = document.getElementById("q-"+productId).value;
        productIdGlobal = productId;
        
        if(quantity > 0){
            ajaxRequest.open("POST", "AddToCart",true); 
            ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ajaxRequest.send("productId="+productId+"&quantity="+quantity); 
        }else
            alert("Quantity Less Than 1");
        
        return false;
}


